﻿
#ifdef MAIN
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )



// hnust_wangkechun 
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <math.h>
#include <algorithm>
#include <memory.h>
#include <utility>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <limits>
using namespace std;

#include <Windows.h>
#include <fstream>
using namespace std;


bool use_outfile=true; //启用输出文件
bool use_debugprint=true;//启用调试窗口打印
bool use_conprint=true;//启用控制台输出
// string output_buf;

double get_now()
{
	LARGE_INTEGER  frequency,start,end;  
	QueryPerformanceFrequency(&frequency);   
	QueryPerformanceCounter(&start);   
	return (double)(start.QuadPart)   /   (double)frequency.QuadPart   *   1000.0; 
}

void printf_g(const char * pszFormat, ...)  
{
	static double last_time;
	//输出缓冲 这里为 100kb
#define MAX_LEN 100*1024
	char szBuf[MAX_LEN];  
	va_list ap;  
	va_start(ap, pszFormat);  
	vsnprintf_s(szBuf, MAX_LEN, MAX_LEN, pszFormat, ap);  
	if(use_conprint) vprintf_s(pszFormat, ap);  
	va_end(ap);  
	if(use_outfile) output_buf+=szBuf;
	//刷新输出时间
	if(use_outfile&&(get_now()-last_time>300)) {
		last_time=get_now();
		ofstream f1("E:\\b.txt");
		f1<<output_buf;
		f1.close();
	}
	if(use_debugprint){
		WCHAR wszBuf[MAX_LEN] = {0};  
		MultiByteToWideChar(CP_UTF8, 0, szBuf, -1, wszBuf, sizeof(wszBuf));  
		OutputDebugStringW(wszBuf);   
	}
} 

void puts_g(char *s)
{
	printf_g("%s\n",s);
}

void putchar_g(char c)
{
	printf_g("%c",char(c));
}

#define printf printf_g
#define puts puts_g
#define putchar putchar_g


int main_g();


int main()
{
	double last_time=get_now();
	
	freopen("E:\\a.txt","r",stdin);

	puts("####START####");
	main_g();
	puts("#####END#####");

	printf( "time:%lf\n" , get_now()-last_time );

	/*
	//性能测试
	use_outfile=false;//禁止输出
	int count=100;//测试次数
	for(int i=0;i<count;i++){
		//多次读入输入文件
		freopen("CON", "w", stdout);
		freopen("E:\\a.txt","r",stdin);
		main_g();
	}
	use_outfile=true;
	printf("\ntimes:%d\n",count);
	printf("all time:%lf\n" , get_now()-last_time );
	*/
	
	//写入剩余的数据
	ofstream f1("E:\\b.txt");
	f1<<output_buf;
	f1.close();
}

#define main main_g

// endif main
#endif




